﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;

namespace HDI.Charts2
{
    public class Pie : Chart
    {


       public  Pie(string Name):base(Name)
        {
            
        }


        public override string getHTML()
        {

            html = "<div id='" + ClientID + "_chartContainer' style='height:"+Height+"' ></div>";
            return html;

        }

        public string getJS()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(String.Format(@"
var {0}_chartData = {1};
			
var {0}_chart = new AmCharts.AmPieChart();
{0}_chart.valueField = 'Value';
{0}_chart.titleField = 'Title';
{0}_chart.dataProvider = {0}_chartData;
{2}
{0}_chart.write('{0}_chartContainer');
", ClientID,  getDataJS(), getOptionJS()));
            return sb.ToString();
        }
        protected string getOptionJS()
        {
            StringBuilder js = new StringBuilder();
            foreach (KeyValuePair<string, string> option in _Options)
            {
                js.Append(String.Format("{0}_chart.{1} = {2};\n", ClientID, option.Key, option.Value));
            }

            return js.ToString(); 
        }
        protected string getDataJS()
        {
            StringBuilder sb = new StringBuilder("["); 
            foreach(Data data in Daten[0] )
            {
                sb.Append("{Title:'" + data.Name+"',Value:"+data["Value"]+"},");
            }
            return sb.Remove(sb.Length - 1, 1).ToString()+"]";
        }

       

        protected override void OnPreRender(EventArgs e)
        {
            JS = getJS();
            base.OnPreRender(e);
            ScriptManager.RegisterClientScriptResource(this, this.GetType(), "HDI.Charts2.js.raphael.js");
            ScriptManager.RegisterClientScriptResource(this, this.GetType(), "HDI.Charts2.js.amcharts.js");
        }
    }
}
